﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using coffeeshop.Models;

namespace coffeeshop.Forms
{
    public partial class frmUserList : Form
    {
        private bool formLoaded = false;


        public frmUserList()
        {
            InitializeComponent();

            LoadUsers(true);
            setButtonEditEnable();
        }

        private void setButtonEditEnable()
        {
            btnEdit.Enabled = listUser.SelectedItems.Count > 0;
        }

        public void ReloadList()
        {
            LoadUsers(ckboxHideInactive.Checked);
            setButtonEditEnable();
        }

        private void LoadUsers(bool toHideInactiveUsers)
        {
            listUser.Clear();
            listUser.ItemCheck += new ItemCheckEventHandler(listUser_ItemCheck);
            listUser.View = View.Details;
            // Display check boxes.
            listUser.CheckBoxes = true;
            // Select the item and subitems when selection is made.
            listUser.FullRowSelect = true;
            // Display grid lines.
            listUser.GridLines = true;
            
            coffeeDataSet.UserDataTable userList = User.LoadUsers(toHideInactiveUsers);
            listUser.Columns.Add("Kích hoạt", 70, HorizontalAlignment.Left);
            listUser.Columns.Add("Tên", 100, HorizontalAlignment.Left);
            listUser.Columns.Add("Chức vụ", 100, HorizontalAlignment.Left);
            listUser.Columns.Add("Nhóm", 100, HorizontalAlignment.Left);
            listUser.Columns.Add("UserId", 0, HorizontalAlignment.Left);
            
            foreach (DataRow row in userList.Rows)
            {
                ListViewItem userItem = new ListViewItem();
                userItem.Checked = (bool)row["IsActive"];
                userItem.SubItems.Add(row["Name"].ToString());
                userItem.SubItems.Add(row["Title"].ToString());
                string roleId = row["UserRoleId"].ToString();
                userItem.SubItems.Add(Global.GetRoleName((Global.UserRoleId)Enum.Parse(typeof(Global.UserRoleId), roleId)));
                userItem.SubItems.Add(row["UserId"].ToString());
                
                listUser.Items.Add(userItem);
            }
        }

        private void listUser_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
        {
            if (!formLoaded)
                return;

            bool isActive = e.CurrentValue == CheckState.Unchecked;

            User theUser = new User(int.Parse(listUser.Items[e.Index].SubItems[4].Text));
            theUser.IsActive = isActive;

            if (!theUser.UpdateActiveness())
            {
                MessageBox.Show("Có lỗi khi thay đổi kích hoạt của nhân viên");
            }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            frmEditUser editForm = new frmEditUser(null);
            editForm.StartPosition = FormStartPosition.CenterParent;
            editForm.MyParentForm = this;
            editForm.ShowDialog(this);
        }

        private void frmUserList_Shown(object sender, EventArgs e)
        {
            formLoaded = true;
        }

        private void ckboxHideInactive_CheckedChanged(object sender, EventArgs e)
        {
            if (!formLoaded)
                return;

            bool isChecked = ((CheckBox)sender).Checked;

            LoadUsers(isChecked);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (listUser.SelectedItems.Count == 0)
            {
                MessageBox.Show("Hãy chọn một nhân viên trước");
                return;
            }
            ListViewItem selectedItem = listUser.SelectedItems[0];
            User theUser = new User(int.Parse(selectedItem.SubItems[4].Text));

            frmEditUser editForm = new frmEditUser(theUser);
            editForm.StartPosition = FormStartPosition.CenterParent;
            editForm.MyParentForm = this;
            editForm.ShowDialog(this);
        }

        private void listUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            setButtonEditEnable();
        }
    }
}
